# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ..items import WxappItem
from scrapy.http.response.html import HtmlResponse
class WxappSpiderSpider(CrawlSpider):
    name = 'wxapp_spider'
    allowed_domains = ['wxapp-union.com']
    start_urls = ['http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1']

    rules = (
        Rule(LinkExtractor(allow=r'.+mod=list&catid=2&page=\d'), follow=True),
        Rule(LinkExtractor(allow=r'.+article-.+\.html'),callback="parse_detail",follow=False),
    )

    def parse_detail(self, response):
        print(type(response))
        #这里的命名不能是parse 因为底层就是执行的parse方法 如果这里明明parse 说明你要重写

        title= response.xpath('//h1[@class="ph"]/text()').get()

        author_p= response.xpath('//p[@class="authors"]')
        author = author_p.xpath('.//a/text()').get()
        pub_time = author_p.xpath('.//span/text()').get()
        aritcle_content = response.xpath("//td[@id='article_content']//text()").getall()
        content = "".join(aritcle_content).strip()
        item = WxappItem(title=title,author=author,pub_time=pub_time,content=content)
        yield item

